#include "logindialog.h"

#include "LoginWidget.h"
#include "signinwidget.h"

#include <QStackedWidget>
#include <QHBoxLayout>

#include <QDebug>
#include <QCloseEvent>

LoginDialog::LoginDialog(QWidget *parent)
    : ElaWidget{parent}
{
    initUI();
    initConnections();
}

LoginDialog::~LoginDialog()
{

}

void LoginDialog::initUI()
{
    setWindowIcon(QIcon(":/resource/Icon/document-e.png"));


    m_stackedWidget = new QStackedWidget(this);

    m_login = new LoginWidget(this);

    m_sigin = new SignInWidget(this);

    m_stackedWidget->addWidget(m_login);
    m_stackedWidget->addWidget(m_sigin);

    m_layout = new QHBoxLayout(this);
    m_layout->addWidget(m_stackedWidget);
}

void LoginDialog::initConnections()
{
    // 信号连接
    connect(m_login, &LoginWidget::switchToSignIn, [this]{
        m_stackedWidget->setCurrentWidget(m_sigin);
    });
    connect(m_sigin, &SignInWidget::switchToLogin, [this]{
        m_stackedWidget->setCurrentWidget(m_login);
    });

    connect(m_login, &LoginWidget::loginRequested, this, &LoginDialog::accepted);

    connect(m_sigin, &SignInWidget::signInRequested, this, &LoginDialog::accepted);
}

void LoginDialog::closeEvent(QCloseEvent *event) {
    qDebug() << "登录窗口关闭";
    emit rejected();
    event->accept();  // 接受关闭操作
    // event->ignore();  // 忽略关闭操作
}
